package com.yjj.demo.spring.redis.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 *
 *
 * @author yangjiajia
 * @time 2021/9/25 18:01
 */
@Service
public class StringService {

    private Logger logger = LoggerFactory.getLogger(StringService.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String key = "test:a";

    public void add(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public void add(String key, String value, long expireTime) {
        stringRedisTemplate.opsForValue().set(key, value, expireTime);
    }

    public void rightPush() {
        logger.info("redis list init");
        stringRedisTemplate.opsForList().rightPushAll(key, "8", "9", "10", "3", "2", "1");
        stringRedisTemplate.opsForList().rightPush(key, "5");
    }

    public int getValue() {
        String value = stringRedisTemplate.opsForList().leftPop(key);
        return Integer.parseInt(value);
    }

}
